--
-- Author: shunguo.chen
-- Date: 2019-05-23 16:15:41
--
-- SkillPanel 技能面板
--

local ResRender = require("app.widget.renders.ResRender")

local SkillRender = import(".renders.SkillRender")
local SkillMorePanel = import(".SkillMorePanel")
local SkillUpBtnPanel = import(".SkillUpBtnPanel")

local ClassRef = app.mvc.createPanelClass(...)

local SKILL_SHOW_NUM = 5
local CHANGE_DEFAULT_SKILL_LV = 5 -- 替换技能默认技能等级=5

--data = 
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node)
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

function ClassRef:remove()
    self._curSkillDescText:release()
    self._nextSkillDescText:release()
    self._runePanel:release()
    if self._morePanel then
        self._morePanel:remove()
    end
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    -- top
    self._resRender = ResRender.create(self._ui.Panel_point)
    self._resRender:setData({type = Enums.TipsType.daibi, id = Enums.lootType.skillPoint, num = 0})
    
    self._specNameText = self._ui["Panel/Panel_top/tlt"]
    self._specTipText = self._ui["Panel/Panel_top/tips"]

    -- mid
    local skillPanel = self._ui.Panel_skill
    local renders = {}
    local render
    for i=1,SKILL_SHOW_NUM do
        render = SkillRender.create(skillPanel:getChildByName("skill_" .. i))
        render:setCanChange(i ~= 1)
        render:setIndex(i)
        render:setClickHandler(function(sender)
            self:_setCurRender(sender)
        end)
        render:setChangeHandler(function(sender)
            executeFunc(self._onOperate, "skill_openMore", sender:getData(), i)
        end)
        renders[i] = render
    end
    self._renders = renders

    self._specTipPanel = skillPanel:getChildByName("Panel_tips")
    self._specIcon = skillPanel:findChild("Panel_tips/bg/icon")
    self._specDescText = skillPanel:findChild("Panel_tips/tips")

    -- btm
    local bomPanel = self._ui.Panel_bom
    self._selectRender = SkillRender.create(bomPanel:getChildByName("icon"))
    self._selectRender:setIgnoreRedPoint(true)
    self._selectRender:unShowNameAndLv()
    self._selectRender:setClickEnable(false)
    self._selectRender:setChooseed(true)

    self._detailListView = bomPanel:getChildByName("ListView")
    self._skillNameText = bomPanel:getChildByName("Text_name")
    self._curSkillDescText = bomPanel:findChild("ListView/Text")
    self._nextSkillDescText = bomPanel:findChild("ListView/Text_1")
    self._runePanel = bomPanel:findChild("ListView/Panel_rune")
    self._runeIcon = self._runePanel:findChild( "bg/icon" )
    self._runeNameText = self._runePanel:getChildByName( "name" )
    self._runeDescText = self._runePanel:getChildByName( "text" )
    self._curSkillDescText:retain()
    self._nextSkillDescText:retain()
    self._runePanel:retain()

    self._chooseBtn = bomPanel:getChildByName("Button_choose")
    self._chooseBtn:addClickEventListener(function()
        if self._curRender then
            executeFunc(self._onOperate, "skill_openMore", 
                self._curRender:getData(), self._curRender:getIndex())
        end
    end)
    local _, bomHeight = bomPanel:getContentSize2()
    self._chooseBtnInitPosY = self._chooseBtn:getPositionY()
    self._chooseBtnMidPosY = bomHeight/2

    self._upBtnPanel = SkillUpBtnPanel.create(bomPanel:getChildByName("Panel_up"))
    self._upBtnPanel:onBtnClick(function(skill)
        if UD:getSkillPoint() > 0 then
            executeFunc(self._onOperate, "skill_lvup", skill)
            self:_playAction()
        else
            display.pushToast(L("lua_code_text_136"))
        end
    end)

    -- more panel
    self._morePanel = SkillMorePanel.create()
    self._morePanel:setPosition(self._ui.Node_more:getPosition())
    self._ui.Panel:addChild(self._morePanel)
    self._morePanel:moveOut()
end

function ClassRef:getGuideNode(id)
    if id == 107 then
        -- 技能提示2，选中第四个技能
        return self._renders[4]:getChildByName("Panel")
    elseif id == 125 then
        -- 技能升级2，选中第二个技能
        return self._renders[2]:getChildByName("Panel")
    elseif id == 126 then
        -- 技能升级3， 点击升级按钮
        return self._upBtnPanel
    elseif id == 127 then
        -- 技能升级4， 查看技能点提示
        return self._resRender
    elseif id == 502 then
        -- 技能切换（技能切换面板第五个技能）
        if self._morePanel:isVisible() then
            return self._morePanel:getGuideNode(id)
        end
    end
end

-- 1 SkillView使用
-- 2 SkillChangeView使用
function ClassRef:setShowType(tp)
    self._showType = tp

    self._ui.Panel_tlt:setVisible(tp == 1)
    self._resRender:setVisible(tp == 1)

    self._morePanel:setShowType(tp)
end

function ClassRef:onOperate(cb)
    self.super.onOperate(self, cb)

    self._morePanel:onOperate(cb)
end

-- 刷新ResRender
function ClassRef:refreshResNum()
    if self._resRender:isVisible() then
        self._resRender:refreshNum()
    end
end

-- 技能有更新，局部刷新
function ClassRef:updateSkill(skill)
    self:refreshResNum()

    local curData = self._curRender:getData()
    if curData and curData:getTeam() == skill:getTeam() then
        self._curRender:setData(skill)
        self:_setCurRender(self._curRender)
    else
        for i,render in ipairs(self._renders) do
            local renderData = render:getData()
            if renderData and renderData:getTeam() == skill:getTeam() then
                render:setData(skill)
                break
            end
        end
    end

    if self._morePanel:isVisible() then
        self._morePanel:updateSkill(skill)
    end
end

-- specType SpecType 专精
-- skills 专精所有技能
-- selectSkillTeam 选中的skillTeam
function ClassRef:refresh(specType, skills, selectSkillTeam)
    self:refreshResNum()

    self._specType = specType

    local curRender, skill
    for i,render in ipairs(self._renders) do
        skill = skills[i]
        render:setData(skill)
        if selectSkillTeam and selectSkillTeam == skill:getTeam() then
            curRender = render
        end
        if self._showType == 2 then
            render:setLevel(CHANGE_DEFAULT_SKILL_LV)
        end
    end
    curRender = curRender or self._renders[1]

    self:_refreshSpecType()
    self:_setCurRender(curRender)
end

-- 专精刷新
function ClassRef:_refreshSpecType()
    local specType = self._specType

    if self._showType == 1 then
        self._specNameText:setString(specType:getNameDes())
        self._specTipText:setString(specType:isActive() and L("spec_state_1") or L("spec_state_2"))
    else
        local dutyShow, roleName = specType:getRoleExtraParams()
        self._specNameText:setString(dutyShow)
        self._specTipText:setString(roleName)
    end

    self._specIcon:loadTexture(specType:getFeatureIcon())
    self._specDescText:setString(specType:getFeatureDes())
    display.setNodesMiddle(self._specTipPanel, nil, self._specIcon:getParent(), self._specDescText)
end

-- 刷新text的size
function ClassRef:_refreshTextSize(text)
    local size = text:getContentSize()
    text:getVirtualRenderer():setDimensions( size.width, 0 )
    local afterSize = text:getVirtualRendererSize()
    text:setContentSize( cc.size( size.width, afterSize.height ) )
end

-- 选择当前查看技能
function ClassRef:_setCurRender(render)
    if self._curRender then
        self._curRender:setChooseed(false)
    end
    render:setChooseed(true)
    self._curRender = render

    local skill = render:getData()
    self._selectRender:setData(skill)
    self._skillNameText:setString(skill:getName())

    self._detailListView:removeAllItems()
    self._curSkillDescText:setString(skill:getDesc())
    self:_refreshTextSize(self._curSkillDescText)
    self._detailListView:pushBackCustomItem(self._curSkillDescText)

    local activeRune = skill:getActiveRune()
    if activeRune then
        self._runeIcon:loadTexture(activeRune:getIcon())
        self._runeNameText:setString(activeRune:getName())
        self._runeDescText:setString(activeRune:getDesc())
        self._detailListView:pushBackCustomItem(self._runePanel)
    end

    self._nextSkillDescText:setString(skill:getNextDesc())
    self:_refreshTextSize(self._nextSkillDescText)
    self._detailListView:pushBackCustomItem(self._nextSkillDescText)

    local isMaxLevel = skill:isMaxLevel()
    self._upBtnPanel:setVisible(not isMaxLevel)
    if not isMaxLevel then
        self._upBtnPanel:setData(skill)
    end

    local canChoose = skill:isLearned() and render:isCanChange()
    self._chooseBtn:setVisible(canChoose)
    if canChoose then
        self._chooseBtn:setPositionY(isMaxLevel and self._chooseBtnMidPosY or self._chooseBtnInitPosY)
    end
end

function ClassRef:openMorePanel(...)
    self._morePanel:moveIn()
    self._morePanel:refresh(...)
end

function ClassRef:_playAction()
    local actionNode = self._actionNode
    local actionTimeline = self._actionTimeline
    if not actionNode then
        actionNode = cc.CSLoader:createNode(Res.upgradeNode,false,false)
        actionTimeline = actionNode:getActionByTag(0)
        actionTimeline:setLastFrameCallFunc(function()
            actionNode:setVisible(false)
        end)
        self._ui.Panel_skill:addChild(actionNode)
    end

    local _, height = self._curRender:getContentSize2()
    actionNode:setPosition(self._curRender:getPositionX(), self._curRender:getPositionY() + height/2)
    actionTimeline:gotoFrameAndPlay(0, false)
end

return ClassRef
